package com.suny.thread_per_message;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class Main {
    public static void main(String[] args) {
        System.out.println("main begin");
        //方式一
//        Host host = new Host();

        //方式二
//        ThreadFactoryHost host = new ThreadFactoryHost(new ThreadFactory() {
//            @Override
//            public Thread newThread(Runnable r) {
//                return new Thread(r);
//            }
//        });

        //方式三
//        ThreadFactoryHost host = new ThreadFactoryHost(Executors.defaultThreadFactory());

        //方式四
//        ExecutorHost host = new ExecutorHost(new Executor() {
//            @Override
//            public void execute(Runnable command) {
//                new Thread(command).start();
//            }
//        });

        //方式五
        /*ExecutorService executorService = Executors.newCachedThreadPool();
        ExecutorHost host = new ExecutorHost(executorService);

        try {
            host.request(10, 'A');
            host.request(20, 'B');
            host.request(30, 'C');
        } finally {
            executorService.shutdown();
            System.out.println("main end");
        }*/

        //方式六：
        //newScheduledThreadPool(5); 创建5个线程存于线程池
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
        ScheduledExecutorServiceHost host = new ScheduledExecutorServiceHost(scheduledExecutorService);
        try {
            host.request(10, 'A');
            host.request(20, 'B');
            host.request(30, 'C');
        } finally {
            scheduledExecutorService.shutdown();
            System.out.println("main end");
        }
    }
}
